package com.yy.yyui.popup
{
	import com.util.tools.ArrayTools;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	public class PopUpManager
	{
		private static var popUpArray:Array = new Array();
		public static const APPLICATION:Object = 0;
		public static const PARENT:Object = 1;
		public static const TARGET:Object = 2;
		public static const POPUP:Object = 3;
		public static const DEFAULT:Object = 4;
		public static var defaultRoot:DisplayObjectContainer;
		
		
		
		public static function popUp(popUpWindow:DisplayObject,
									 target:DisplayObjectContainer,
									 modal:Boolean = true,
									 modalEffect:Boolean = false,
									 addChildList:Object = PopUpManager.TARGET,
									 tween:Boolean=false,
									 dragState:Boolean = false):DisplayObjectContainer{
			if(target == null)
			{
				return null;
			}
			if(defaultRoot==null){
				defaultRoot = target.root as DisplayObjectContainer;
			}
			var container:DisplayObjectContainer;
			switch(addChildList){
				case POPUP:
					container = findPopUpWindow(target);
					if(container == null) {
						container = defaultRoot; 
					}
					break;
				case PARENT:
					container = target.parent;
					break;
				case TARGET:
					container = target;
					break;
				case APPLICATION:
					container = target.stage;
					break;
				case DEFAULT:
				default:
					container = defaultRoot;
					break;				
			}
			var dobj:DisplayObjectContainer = new PopUpWindow(popUpWindow,container,modal,modalEffect,dragState);
			var obj:Object = new Object();
			obj.popUpWindow = popUpWindow;
			obj.container = container;
			obj.frame = dobj;
			obj.target = target;
			obj.type = addChildList;
			obj.instance = popUpWindow.hasOwnProperty("instance")?(popUpWindow as Object).instance:false;
			
			popUpWindow.addEventListener(Event.REMOVED_FROM_STAGE,unregister);
			obj.childDisplay = container.addChild(dobj);
			popUpArray.push(obj);
			return dobj;
		}
		
		private static function findObjectByPopUpWindow(popUpWindow:DisplayObject):Object{
			var i:int,l:int;
			l = popUpArray.length;
			for(i = 0;i<l;i++){
				var obj:Object = popUpArray[i];
				if(obj!=null&&(obj.popUpWindow==popUpWindow||obj.frame==popUpWindow)){
					return obj;
				}
			}
			return null;
		}
		
		private static function unregister(e:Event):void{
			var obj:Object = findObjectByPopUpWindow(e.currentTarget as DisplayObject);
			if(obj!=null){
				ArrayTools.del(popUpArray,obj);
				if(obj.type==POPUP){
					obj.target.filters = null;
				}
			}
			
		}
		
		
		
		public static function closePopupWindow(popUpWindow:DisplayObject):void{
			var obj:Object = findObjectByPopUpWindow(popUpWindow);
			if(obj!=null){
				if(obj.instance)
				{
					(popUpWindow as Object).setVisible(false);
					return;
				}
				var dobj:DisplayObject = obj.childDisplay as DisplayObject;
				dobj.parent.removeChild(dobj);
			}
		}		
		
		public static function findPopUpWindow(target:DisplayObject):PopUpWindow{
			var obj:DisplayObject = target;
			while(obj!=null){
				if(obj is PopUpWindow) return obj as PopUpWindow;
				else obj = obj.parent;				
			}
			return null;
		}
		public static function removeAllWindow():void
		{
			for each(var window:Object in popUpArray)
			{
				closePopupWindow(window.popUpWindow);
				if(window.hasOwnProperty("instance"))
				{
					if((window as Object).instance)
					{
						
					}else
					{
						ArrayTools.del(popUpArray,window);
					}
				}else
				{
					ArrayTools.del(popUpArray,window);
				}
			}
		}
	}
}